package rabbitMQ;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import rabbitMQ.entity.User;

import java.util.Date;
import java.util.UUID;

@RestController
public class ProviderController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("aa")
    public String aa() throws JsonProcessingException {
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);

        User user = new User();
        user.setAge(10);
        user.setName("zs");
        user.setTime(new Date());
        String s = new ObjectMapper().writeValueAsString(user);
        Message message = new Message(s.getBytes(), messageProperties);

        rabbitTemplate.send("ltTestExchange", "ltTestRoutingKey", message, correlationData);
        return "OK";
    }


}
